package traveldream.ejb.sessionBean;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TemporalType;

import traveldream.ejb.DTO.ExcursionDTO;
import traveldream.ejb.DTO.HotelDTO;
import traveldream.ejb.DTO.TransportationDTO;
import traveldream.ejb.entities.Excursion;
import traveldream.ejb.entities.Hotel;
import traveldream.ejb.entities.TransportationMean;
import traveldream.ejb.interfaces.PackageCustomizeBeanLocal;

/**
 * Session Bean implementation class PackageCustomizeBean
 */
@Stateless
public class PackageCustomizeBean implements PackageCustomizeBeanLocal {

	@PersistenceContext
	private EntityManager em;
	
    /**
     * Default constructor. 
     */
    public PackageCustomizeBean() {
        // TODO Auto-generated constructor stub
    	
    }

	@Override
	public List<HotelDTO> Hotel(int selectedG, int selectedR, int n) {
		// TODO Auto-generated method stub
		System.out.println(selectedG+"-"+selectedR);
		TransportationMean transpork=em.find(TransportationMean.class, selectedG);
		TransportationMean transport=em.find(TransportationMean.class, selectedR);
		Query query=em.createNamedQuery("Hotel.findNotBooked", Hotel.class);
		query.setParameter("citySelected", transpork.getArrival_Place());
		query.setParameter("dep_date", transpork.getArrival_Date());
		query.setParameter("arr_date", transport.getDeparture_Date());
		List<Hotel> hotel = query.getResultList();
		query=em.createNamedQuery("Booking.checkAvl", Hotel.class);
		query.setParameter("arr_place", transpork.getArrival_Place());
		query.setParameter("dep_date", transpork.getArrival_Date());
		query.setParameter("arr_date", transport.getDeparture_Date());
		query.setParameter("n", n);
		hotel.addAll(query.getResultList());
		List<HotelDTO> hDTO=new ArrayList<HotelDTO>();
		for(int i=0;i<hotel.size();i++)
		{
			hDTO.add(hotel.get(i).convertToDTO());
		}
		return hDTO;
	}

	@Override
	public List<TransportationDTO> going(String city, int n) {
		// TODO Auto-generated method stub
		Query query=em.createNamedQuery("TransportationMean.findArrival", TransportationMean.class);
		query.setParameter("arr_place", city);
		query.setParameter("n", n);
		List<TransportationMean> transport = query.getResultList();
		List<TransportationDTO> tDTO=new ArrayList<TransportationDTO>();
		for(int i=0;i<transport.size();i++)
		{
			tDTO.add(transport.get(i).convertToDTO());
		}
		return tDTO;
	}

	@Override
	public List<TransportationDTO> ret(int selectedG, int n) {
		// TODO Auto-generated method stub
		TransportationMean transpork = em.find(TransportationMean.class, selectedG);
		Query query=em.createNamedQuery("TransportationMean.findReturn", TransportationMean.class);
		query.setParameter("arr_place", transpork.getDeparture_Place());
		query.setParameter("dep_place", transpork.getArrival_Place());
		query.setParameter("date_goi", transpork.getArrival_Date(),TemporalType.DATE);
		query.setParameter("n", n);
		List<TransportationMean> transport = query.getResultList();
		List<TransportationDTO> tDTO=new ArrayList<TransportationDTO>();
		for(int i=0;i<transport.size();i++)
		{
			tDTO.add(transport.get(i).convertToDTO());
		}
		return tDTO;
	}

	@Override
	public List<ExcursionDTO> excursion(int selectedG, int selectedR, int n) {
		// TODO Auto-generated method stub
		TransportationMean transpork=em.find(TransportationMean.class, selectedG);
		TransportationMean transport=em.find(TransportationMean.class, selectedR);
		Query query=em.createNamedQuery("Excursion.checkAvl", Excursion.class);
		query.setParameter("arr_place", transpork.getArrival_Place());
		query.setParameter("dep_date", transpork.getArrival_Date());
		query.setParameter("arr_date", transport.getDeparture_Date());
		query.setParameter("n", n);
		List<Excursion> excursion = query.getResultList();
		List<ExcursionDTO> eDTO = new ArrayList<ExcursionDTO>();
		for(int i=0;i<excursion.size();i++)
		{
			eDTO.add(excursion.get(i).convertToDTO());
		}
		return eDTO;
	}

}
